home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / pascal / tpref.exe / TPR3A.TXT < prev    next >
Text File  |  1992-10-19  |  60KB  |  1,461 lines

  1.                                    Chapter 3
  2.                  - Part 1 of 3 parts -
  3.                                     of the
  4.                             Turbo Pascal Reference
  5.  
  6.                            The Turbo Pascal Language
  7.  
  8.  
  9. This chapter is part of the Turbo Pascal Reference electronic freeware book (C)
  10. Copyright 1992 by Ed Mitchell.  This freeware book contains supplementary
  11. material to Borland Pascal Developer's Guide, published by Que Corporation,
  12. 1992.  However, Que Corporation has no affiliation with nor responsibility for
  13. the content of this free book.  Please see Chapter 1 of the Turbo Pascal
  14. Reference for important information about your right to distribute and use this
  15. material freely.  If you find this material of use, I would appreciate your
  16. purchase of one my books, such as the Borland Pascal Developer's Guide or
  17. Secrets of the Borland C++ Masters, Sams Books, 1992.  Thank you.
  18.  
  19. Note:  For ease of access, Chapter 3 is continued in TPR3B.TXT and TPR3C.TXT.
  20.  
  21.  
  22.      The Pascal programming language was originally developed by Niklaus Wirth
  23. as an idealized language for teaching basic concepts of programming.  Many of
  24. the concepts and constructions used in Pascal trace their ancestry to the Algol
  25. programming language developed in the early 1960's.  By 1968, Wirth had
  26. developed the basic structure of Pascal, but it was not until his publication
  27. of the Pascal User Manual and Report in 1973 that Pascal received wide spread
  28. acceptance.
  29.      Since that time, the core structure of Pascal has remained largely the
  30. same, but many signficant enhancements have been made, especially in the
  31. version known as Turbo Pascal.  These enhancements include:
  32.  
  33.        Units, for the sharing and reuse of code, 
  34.  
  35.        Powerful object oriented programming capabilities,
  36.  
  37.        Extensive library support including graphics, overlays (for managing
  38.        large programs), and system-level access for system programming.
  39.  
  40.      Yet, because Turbo Pascal traces its heritage to Pascal's early
  41. development as a tool for teaching programming, Turbo Pascal is probably the
  42. premier programming language for learning modern programming concepts and
  43. practices.  And with Borland's significant enhancements, Turbo Pascal has
  44. become the definitive implementation of the Pascal language, providing  one of
  45. the most powerful development languages and environments available.  The
  46. professional editions of Turbo Pascal and Borland Pascal are every bit as
  47. powerful as C++, plus Borland's Pascal compilers generate fast, compact code,
  48. often much smaller than similar C++ code.  Except for Pascal's lack of
  49. templates and overloaded functions, Turbo Pascal has all the features and
  50. capabilities of C++.
  51.      This chapter presents an overview of the entire Turbo Pascal language,
  52. except for object oriented programming (OOP) features.  OOP may be found in
  53. Chapter 4, "Object Oriented Programming" in the Borland Pascal Developer's
  54. Guide.
  55.  
  56.  
  57. Your First Turbo Pascal Program
  58.      Common practice is to introduce a new programming language by writing a
  59. simple program that simply displays,
  60.  
  61.      Hello, World!
  62.  
  63. In Turbo Pascal, such a program look liks this:
  64.  
  65.      program Hello;
  66.      begin
  67.           Writeln('Hello, World!');
  68.      end.
  69.  
  70. Each Pascal program begins with a statement containing the keyword program and
  71. the program name, shown here as Hello, optional variable declarations (none
  72. shown in this example), the keyword begin, zero or more Pascal statements, and
  73. an end statement to terminate the program.  Each statement is terminated with a
  74. semicolon, except the last end statement is terminated in a period.
  75.      The statement,
  76.  
  77.      Writeln('Hello, World!');
  78.  
  79. calls a built-in Pascal routine called Writeln, which has as its single
  80. parameter, the contents of the text to be printed.
  81.      All Pascal programs have roughly the same structure as that used by the
  82. Hello program, but with many more options available.
  83.      While the Turbo Pascal Reference includes substantial tutorial and
  84. reference information, if you have little or no programming experience, I
  85. suggest you consult an introductory text first.
  86.      Programmers who are experienced in Pascal or other programming languages
  87. such as C, QuickBasic or Visual Basic, will find that Turbo Pascal Reference
  88. provides a concise and complete description of the Turbo Pascal language,
  89. libraries, development environment and the powerful Turbo Vision
  90. character-based windowing system.
  91.  
  92.  
  93. Pascal Program Structure
  94.      Each Pascal program is organized in a standard format, as shown in figure
  95. 3.1.  Turbo Pascal, unlike other implementations of Pascal, does not require
  96. the strict ordering of declarations suggested by figure 3.1.  In Turbo Pascal,
  97. you can, if you wish, place type declarations before the const declarations, or
  98. even add a second var section after you have defined procedures and functions. 
  99. You do not need to have each of the declarations sections shown, but only those
  100. that are used by your program.  Figure 3.2 displays a "railroad" syntax diagram
  101. illustrating how the declarations may occur in any order.  These syntax
  102. diagrams are frequently used to describe the structure of Pascal programs.  You
  103. read the diagram by starting, usually, at the upper left corner and tracing
  104. through the drawing, through each keyword or symbol in the language, until you
  105. exit the diagram, usually a the upper right.
  106.  
  107. Figure 3.1.  The structure of Pascal program.
  108.  
  109.      program programname;
  110.      uses
  111.        unitnames-list;
  112.      label
  113.        declarations;
  114.      const
  115.        declarations;
  116.      type
  117.        declarations;
  118.      var 
  119.        variable declarations;
  120.      procedure and function declarations;
  121.  
  122.      begin
  123.        Main body of program;
  124.      end.
  125.  
  126. ***03tpr02.pcx***
  127. Figure 3.2.  A syntax diagram showing how the elements of the Pascal language
  128. declarations may appear in any order.
  129.  
  130.  
  131. Pascal Data Types
  132.      Data types determine the type of data and the permissible range of values
  133. within each type upon which Pascal can operate.   These data types range from a
  134. variety of small and large integer values to several real or floating point
  135. number formats, plus character and string values.  The basic data types of
  136. Pascal are shown in Table 3.1.
  137.      For reasons of program efficiency, some values may be represented by more
  138. than one type.  Consider a numeric value in the range of 0 to 100.  Such a
  139. value can be represented by an Integer, Byte, Shortint, Longint or other
  140. standard types.    These values vary in the amount of memory storage they
  141. require, and hence, the length of time it takes the CPU to process such data. 
  142. For example, the Byte data type uses 8 bits of memory, while the Longint type
  143. takes 32 bits.  
  144.      Table 3.1 presents the standard predefined Turbo Pascal data types.  As
  145. described later in this chapter, you may also create custom data types, as
  146. appropriate.
  147.  
  148.  
  149. Table 3.1.  The standard Pascal data types.
  150. Data Type          Typical Values              Size and Description
  151.  
  152. Boolean  True, False         1 byte
  153.           A Boolean value holds one of the predefined constants True or False,
  154.           where Ord(True) = 1 and Ord(False) = 0.
  155.  
  156. Byte     0 to +255           1 byte
  157.           A Byte value is an integer in the range of 0 to +255 and is
  158.           represented in an unsiged 8 bit format.
  159.  
  160. Shortint -128 to +127        1 byte
  161.           A numeric value in the range -128 to +127 is a signed 8-bit value
  162.           called a Shortint.
  163.  
  164. Integer  -32768 to +32767    2 bytes
  165.           Integers store numeric values in the range of -32768 to +32767 in
  166.           signed 16 bit format.  
  167.  
  168. Word     0 to 65535          2 bytes
  169.           The Word data type is an unsigned 16 bit value and is used when
  170.           numbers larger than standard integers are required, and when negative
  171.           values are not needed.
  172.  
  173. Longint  -2147483648 to 2147483647  4 bytes
  174.           Longints store signed 32-bit values and are used for storing very
  175.           large integer values.  They may also be used for high speed fixed
  176.           point operations fo